﻿using UnityEngine;
using System.Collections;

[System.Serializable]
public class SpriteBind
{
	public KeyCode bindedKey;
	public SpriteState bindedSpriteState;
}

public class EmoticonManager : MonoBehaviour {
	
	SpriteState smile;
	SpriteState grin;
	SpriteState cry;
	SpriteState shock;
	SpriteState curious;
	SpriteState angry;
	SpriteState love;
	SpriteState tearDrop;
	SpriteState thanks;
	SpriteState help;
	SpriteState thumbsUp;
	SpriteState thumbsDown;
	
	public SpriteBind[] bindedSprite;
	bool isPlaying;
	Sprites spriteHandler;
	SpriteState currentState;
	float appearCounter;
	// Use this for initialization
	void Start () 
	{
		int targetFps = 4;
		spriteHandler = new Sprites(this.renderer,new Vector2(87, 92), 0, 0);
		smile    = new SpriteState(0, 3,targetFps, false);
		grin     = new SpriteState(4, 7,targetFps, false);
		cry      = new SpriteState(8, 11,targetFps, false);
		shock    = new SpriteState(12, 15,targetFps, false);
		curious  = new SpriteState(16, 19,targetFps, false);
		angry    = new SpriteState(20, 23,targetFps, false);
		love     = new SpriteState(24, 27,targetFps, false);
		tearDrop = new SpriteState(28, 31,targetFps, false);
		thanks   = new SpriteState(32, 35,targetFps, false);
		help     = new SpriteState(36, 39,targetFps, false);
		thumbsUp = new SpriteState(40, 43,targetFps, false);
		thumbsDown = new SpriteState(44, 47,targetFps, false);
		
		bindedSprite[0].bindedSpriteState = smile;
		bindedSprite[1].bindedSpriteState = grin;
		bindedSprite[2].bindedSpriteState = cry;
		bindedSprite[3].bindedSpriteState = shock;
		bindedSprite[4].bindedSpriteState = curious;
		bindedSprite[5].bindedSpriteState = angry;
		bindedSprite[6].bindedSpriteState = love;
		bindedSprite[7].bindedSpriteState = tearDrop;
		bindedSprite[8].bindedSpriteState = thanks;
		bindedSprite[9].bindedSpriteState = help;
		bindedSprite[10].bindedSpriteState = thumbsUp;
		bindedSprite[11].bindedSpriteState = thumbsDown;
		
		int _baseKey = (int)KeyCode.F1;
		for(int i = 0; i < bindedSprite.Length; i++)
		{
			bindedSprite[i].bindedKey = (KeyCode)(_baseKey + i);
		}
		renderer.enabled = false;
	}
	
	IEnumerator startEmoticon()
	{
		renderer.enabled = true;
		yield return new WaitForSeconds(1.3f);
		renderer.enabled = false;
		isPlaying = false;
	}
	
	void handleInput(int index)
	{
		//if(isPlaying)
		//	return;
		sendEmoticon(index);
		networkView.RPC("sendEmoticon",RPCMode.Others,index);
	}
	
	[RPC]
	void sendEmoticon(int index)
	{
		
		SpriteState stateToPlay = bindedSprite[index].bindedSpriteState;
		if(stateToPlay == currentState)
			return;
		
		currentState = stateToPlay;
		
		//StartCoroutine(startEmoticon());
		spriteHandler.Play(stateToPlay);
		isPlaying = true;
		renderer.enabled = true;
		appearCounter = 0;
	}
	
	
	// Update is called once per frame
	void Update () 
	{
		if(spriteHandler.active)
			spriteHandler.Update();
		
		if(networkView.isMine)
		{
			for(int i =0; i< bindedSprite.Length; i++)
			{
				if(Input.GetKeyDown(bindedSprite[i].bindedKey))
				{
					handleInput(i);
				}
			}
		}
		
		if(isPlaying)
		{
			appearCounter += Time.deltaTime;
			if(appearCounter >= 1.3f)
			{
				renderer.enabled = false;
				spriteHandler.active = false;
				isPlaying = false;
				currentState = null;
			}
		}
	}
}
